package com.example.demo.dao.system;

import com.example.demo.model.system.User;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface UserDAO {

//  一对多查询
    @Select("<script> " +
            "select * from user where role_id = #{roleId}" +
            "</script>")
    List<User> selectByRoleId(Integer roleId);

//    一对一查询
    @Select("select * from user ")
    @Result(column = "role_id", property = "role",
        one = @One(select = "com.example.demo.dao.system.RoleDAO.getByRoleId",fetchType = FetchType.LAZY))
    public List<User> selectAll() ;

//  模糊查询
    @Select("<script>"+
            "select * from user where" +
            "<if test=\"role_id !=null or role_id != ''\">"+
            "role_id=#{role_id}"+
            "</if>"+
            "<if test=\"username !=null or username != ''\">"+
            "and username like CONCAT('%',#{username},'%')"+
            "</if>"+
            "</script>")
//    @Select("select * from user where username like CONCAT('%',#{username},'%') and role_id = #{role_id}")
    @Result(column = "role_id", property = "role",
            one = @One(select = "com.example.demo.dao.system.RoleDAO.getByRoleId",fetchType = FetchType.LAZY))
    public List<User> findUser(String  username,Integer role_id) ;

//    添加用户
    @Insert("insert into user(role_id,username,password) values(#{role_id},#{username},#{password})")
    Integer addUser(User user);

//    删除用户
    @Delete("delete from user where user_id=#{userId}")
    Integer deleteById(Integer userId);

//    修改用户
    @Update("update user set role_id=#{role_id},username=#{username},password=#{password} where user_id=#{user_id} ")
    Integer updateById(User user);

//登录功能

    @Select("select * from user where username=#{username} and password=#{password}")
    Boolean login(String username,String password);
//    User login(User user);
}